package ncut.shumei.controller;

import io.swagger.annotations.*;
import ncut.shumei.dto.*;
import ncut.shumei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/users")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户列表")
    @GetMapping
    public ResponseEntity<List<UserDTO>> getUsers(
            @ApiParam(value = "用户角色", example = "USER/ADMIN") @RequestParam(required = false) String role,
            @ApiParam(value = "用户状态(0:禁用,1:启用)", example = "1") @RequestParam(required = false) Integer status) {
        return ResponseEntity.ok(userService.getUsers(role, status));
    }

    @ApiOperation("获取用户详情")
    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUser(@PathVariable Integer id) {
        return ResponseEntity.ok(userService.getUser(id));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResponseEntity<UserDTO> register(@RequestBody UserCreateRequest request) {
        return ResponseEntity.ok(userService.createUser(request));
    }

    @ApiOperation("创建用户")
    @PostMapping
    public ResponseEntity<UserDTO> createUser(@RequestBody UserCreateRequest request) {
        return ResponseEntity.ok(userService.createUser(request));
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/{id}")
    public ResponseEntity<UserDTO> updateUser(
            @PathVariable Integer id,
            @RequestBody UserUpdateRequest request) {
        return ResponseEntity.ok(userService.updateUser(id, request));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("修改用户状态")
    @PutMapping("/{id}/status")
    public ResponseEntity<Void> updateStatus(
            @PathVariable Integer id,
            @RequestParam Integer status) {
        userService.updateUserStatus(id, status);
        return ResponseEntity.ok().build();
    }
} 